package com.xh.audit.controller.unituser;

import com.mybatisflex.core.paginate.Page;
import com.xh.audit.entity.unituser.AuditUsers;
import com.xh.audit.service.AuditUsersService;
import com.xh.audit.vo.webform.AuditUsersQuery;
import com.xh.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 审计人员 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/audit-users")
@RequiredArgsConstructor
@Validated
public class AuditUsersController {
    private final AuditUsersService auditUsersService;


    @PostMapping("/save")
    public R<AuditUsers> save(@RequestBody
                              @Validated
                                      AuditUsers tosave) {
        boolean saveFlag = auditUsersService.saveOrUpdate(tosave);

        if (saveFlag) {
            return R.success(auditUsersService.getById(tosave.getId()));
        }
        return R.failed("保存失败");
    }

    @PostMapping("/queryByPage")
    public R<Page<AuditUsers>> query(@RequestBody
                                     @Validated
                                             AuditUsersQuery queryPage) {

        Page<AuditUsers> page1 = auditUsersService.page(queryPage.getPageInfo(), queryPage.queryWrapperBuild());
        return R.success(page1);
    }

    @PostMapping("/info/{id}")
    public R<AuditUsers> info(@PathVariable
                              @Validated
                              @NotNull(message = "id不能空")
                                      Integer id) {

        AuditUsers byId = auditUsersService.getById(id);
        return R.success(byId);
    }

    @PostMapping("/del")
    public R<AuditUsers> del(@RequestBody
                             @Validated
                             @NotEmpty(message = "ids 集合不能空")
                                     List<Integer> ids) {
        auditUsersService.removeByIds(ids);
        return R.success("删除成功");
    }
}
